#!/usr/bin/env python

#originally from: http://matplotlib.sourceforge.net/examples/pylab_examples/legend_scatter.html

from pylab import *
from collections import namedtuple
import csv

N=1000

#props = dict( alpha=0.5, edgecolors='none' )
props = dict( alpha=0.25, edgecolors='none' )

def get_x(school):
    return float(school.longitude)

def get_y(school):
    return float(school.latitude)

def get_radius(school, accessor= lambda x: x.total):
    return float(accessor(school))/5.0


School = namedtuple('School', 'name, latitude, longitude, total, asian, black, hispanic, nativeamerican, white')

schools = []
for row in csv.reader(open("results.dat", "rb")):
    try:
        school = School._make(row)
    except TypeError:
        continue
    schools.append(school)

xs = [get_x(school) for school in schools]
ys = [get_y(school) for school in schools]
#totals
#ss = [get_radius(school) for school in schools]
#scatter(xs, ys, c='red', s=ss, **props)


colors = [(lambda x: x.asian, '#0093BB', 'Asian'), 
          (lambda x: x.black, '#93B216', 'Black'), 
          (lambda x: x.hispanic, '#F5D600', 'Hispanic'), 
          #(lambda x: x.nativeamerican, '#F5D600', 'Native American'), 
          (lambda x: x.white, '#683B1A', 'White')]

handles = []
for accessor, color, label in colors:
    ss = [get_radius(school, accessor) for school in schools]
    point = scatter(xs, ys, c=color, s=ss, label=label, **props)
    handles.append(point)

legend(handles, [x[2] for x in colors])
#legend(handles)
#legend(('Asian'))


# handles = []
# colours = ['red', 'green', 'blue', 'magenta', 'cyan', 'yellow']
# colours = ['red', 'green', 'blue']
# for colour in colours:
#     print "asdf"
#     x, y = rand(2,N)
#     s = 400.0 * rand(N)
#     point = scatter(x, y, c=colour, s=s, **props)
#     handles.append(point)

#legend(handles, colours)
#grid(True)

show()
